package com.yunhai.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.model.entity.ums.UmsRole;
import com.yunhai.model.entity.ums.UmsRolePermission;
import com.yunhai.ucenter.mapper.SysMenuMapper;
import com.yunhai.ucenter.mapper.SysRoleMapper;
import com.yunhai.ucenter.mapper.SysRoleMenuMapper;
import com.yunhai.ucenter.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author mengze
 * @since 2023-08-23
 */
@Service
public class SysRoleServiceImpl  implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    @Transactional
    public void addRole(UmsRole sysRole) throws Exception {

        //如果新增角色中设置了权限
        sysRoleMapper.insert(sysRole);
        if( ObjectUtil.isNotEmptyList(sysRole.getPermissionIdList()) ){
            for( Long permissionId: sysRole.getPermissionIdList() ){
                UmsRolePermission sysRoleMenu = new UmsRolePermission();
                sysRoleMenu.setRoleId(sysRole.getId());
                sysRoleMenu.setMenuId(permissionId);
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        }
        return;
    }

    @Override
    @Transactional
    public void updateRole(UmsRole sysRole) throws Exception {

        sysRoleMapper.updateById(sysRole);
        if( sysRole.getPermissionIdList() == null || sysRole.getPermissionIdList().size() == 0 ){
            return;
        }

        //先删除原来的 role-menu
        LambdaQueryWrapper<UmsRolePermission> deleteLqs = new LambdaQueryWrapper<>();
        deleteLqs.eq( UmsRolePermission::getRoleId, sysRole.getId() );
        sysRoleMenuMapper.delete(deleteLqs);

        //插入新的权限
        for( Long permissionId: sysRole.getPermissionIdList()){
            UmsRolePermission sysRoleMenu = new UmsRolePermission();
            sysRoleMenu.setRoleId(sysRole.getId());
            sysRoleMenu.setMenuId(permissionId);
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
        return;
    }

    @Override
    public void batchRemoveRole(List<Long> ids) throws Exception {
        sysRoleMapper.deleteBatchIds(ids);
        return;
    }


    @Override
    public IPage paginationRoleList(PaginationVo paginationVo) throws Exception {

        IPage page = new Page(paginationVo.getPageIndex(), paginationVo.getPageSize());
        LambdaQueryWrapper<UmsRole> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByDesc( UmsRole::getCreateTime );
        sysRoleMapper.selectPage(page, lambdaQueryWrapper);

        List<UmsRole> records = page.getRecords();
        List<Long> roleIds = records.stream().map(item -> item.getId()).collect(Collectors.toList());
        if( roleIds == null || roleIds.size()  == 0 ){
            return page;
        }

        LambdaQueryWrapper<UmsRolePermission> roleLqs = new LambdaQueryWrapper();
        roleLqs.in( UmsRolePermission::getRoleId, roleIds );
        List<UmsRolePermission> sysRoleMenus = sysRoleMenuMapper.selectList(roleLqs);
        if( sysRoleMenus == null || sysRoleMenus.size() == 0 ){
            return  page;
        }


//        Set<Long> menuIds = sysRoleMenus.stream().map(item -> item.getMenuId()).collect(Collectors.toSet());
//        List<UmsPermission> sysMenus = sysMenuMapper.selectBatchIds(menuIds);
        for( UmsRole sysRole : records ){
            Set<Long> curRolePermissionIdList = sysRoleMenus.stream().filter(item -> item.getRoleId().equals(sysRole.getId())).map(item -> item.getMenuId()).collect(Collectors.toSet());
            sysRole.setPermissionIdList( new ArrayList<Long>(curRolePermissionIdList));
//            List<UmsPermission> curRoleMenuList = new ArrayList<UmsPermission>();
//            for( Long permissionId : curRolePermissionIdList ){
//                List<UmsPermission> menuList = sysMenus.stream().filter(item -> item.getId().equals(permissionId)).collect(Collectors.toList());
//                curRoleMenuList.addAll(menuList);
//            }
//            sysRole.setPermissionList(curRoleMenuList);
        }

        return page;
    }

    @Override
    public List<UmsRole> allRoleList() throws Exception {

        List<UmsRole> sysRoles = sysRoleMapper.selectList(null);
        return sysRoles;
    }
}
